package asteroids.model.expressions;

import java.util.*;
import asteroids.model.types.*;

/**
 * A subclass of expression (E) that represents all the comparisons off booleans
 * 
 * @version 1.0
 * 
 * @author	Thijs Peirelinck & Wouter Parys
 *
 */
public abstract class BooleanComparison extends Comparison {

	public BooleanComparison(E e1, E e2) {
		super(e1, e2);
	}
	
	@Override
	public boolean isValidExpression() {
		return (getLeftOperand().getReturnType().equals("boolean") && getRightOperand().getReturnType().equals("boolean") && !(getLeftOperand() == this) && !(getRightOperand() == this));
	}

	@Override
	public boolean typeCheck(Map<String, T> globals) {
		boolean test1 = super.typeCheck(globals);
		boolean test2;
		//its enough the check if one of the operands has returnType "boolean"
		if(getLeftOperand() instanceof Variable) {
			if(globals.get(getLeftOperand().getValue()) == null)
				return false;
			test2 = globals.get(getLeftOperand().getValue()).getReturnType().equals("boolean");
		}
		else 
			test2 = getLeftOperand().getReturnType().equals("boolean");
		return test1 && test2;
	}

}
